//
//  JMBCouponFinishedVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/8/8.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "JMBCouponFinishedVC.h"
#import "JMBCouponFinishedCell.h"
@interface JMBCouponFinishedVC ()

@end

@implementation JMBCouponFinishedVC

/// 重写init方法，配置你想要的属性
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.shouldPullDownToRefresh = YES;
        self.shouldPullUpToLoadMore = YES;
        self.shouldEndRefreshingWithNoMoreData = NO;
        [self.tableView registerClass:[JMBCouponFinishedCell class] forCellReuseIdentifier:@"JMBCouponFinishedCell"];
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
}

#pragma mark - Override
- (void)tableViewDidTriggerHeaderRefresh{
    [MBProgressHUD hideHUDForView:self.view];
    NSInteger count = (self.page >= 3)?18:self.perPage;
    for (NSInteger i = 0; i < count; i++) {
        NSString *title = [NSString stringWithFormat:@"这是第%ld条优秀数据",(long)(i + (self.page - 1) * self.perPage)];
        [self.dataSource addObject:title];
    }
    [self.tableView jm_configEmptyViewWithType:JMEmptyDataViewTypeDefault emptyInfo:nil errorInfo:nil offsetTop:250 hasData:self.dataSource.count>0 hasError:NO reloadBlock:NULL];
    /// 告诉系统你是否结束刷新 , 这个方法我们手动调用，无需重写
    [self tableViewDidFinishTriggerHeader:YES reload:YES];
}

- (void)tableViewDidTriggerFooterRefresh{
    /// 下拉加载事件 子类重写
    self.page = self.page + 1;
    
    /// 模拟网络
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        /// hid HUD
        [MBProgressHUD hideHUDForView:self.view];
        
        /// 假设第3页的时候请求回来的数据 < self.perPage 模拟网络加载数据不够的情况
        NSInteger count = (self.page >= 3)?18:self.perPage;
        /// 模拟数据
        for (NSInteger i = 0; i < count; i++) {
            NSString *title = [NSString stringWithFormat:@"这是第%ld条优秀数据",(long)(i + (self.page - 1) * self.perPage)];
            //            CMHExampleTableTest * et = [[CMHExampleTableTest alloc] init];
            //            et.idNum = i + (self.page - 1) * self.perPage;
            //            et.title = title;
            [self.dataSource addObject:title];
        }
        /// 告诉系统你是否结束刷新 , 这个方法我们手动调用，无需重写
        [self tableViewDidFinishTriggerHeader:NO reload:YES];
        
    });
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JMBCouponFinishedCell *cell =[JMBCouponFinishedCell dequeueCellWithTableView:tableView indexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 205;
}
@end
